/**
 * Created by BaiYJ on 2017/4/21.
 */

// $(document).ready(function() {
//     $('#houseTable').DataTable();
// } );
var editor; // use a global for the submit and return data rendering
var table;
var userTypeId = 0;
function getUrl(url, params) {
    for (var i = 0; i < params.length; i++) {
        if ($("#" + params[i]).val() != "") {
            url = url + "&" + params[i] + "=" + $("#" + params[i]).val();
            }
    }
    return url;

}
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]);
    return null; //返回参数值
}
$(document).ready(function () {

    var communityId = getUrlParam("communityId");
    $.ajax({
        url: '/commis/api/user/currentUser',
        type: "GET",
        async: false,
        dataType: 'json',
        data: {},
        success: function (response) {
            if (response.status == 0) {
                console.log("用户存在");
                var user = response.data;
                userTypeId = user.userTypeId;
                console.log(userTypeId);
            } else if (response.status == 1) {
                alert("登录用户已经过期，请重新登录");
                window.parent.location.href = "../../login.html";
            }
        }, error: function (msg) {
            alert("登录用户已经过期，请重新登录");
            window.parent.location.href = "../../login.html";
        }
    })


    editor = new $.fn.dataTable.Editor({
        ajax: {
            create: {
                type: 'POST',
                url: '/commis/api/communityInfo/create?communityId=' + communityId
            },
            edit: {
                type: 'PUT',
                url: '/commis/api/communityInfo/edit?id=_id_&communityId=' + communityId
            },
            remove: {
                type: 'DELETE',
                url: '/commis/api/communityInfo/remove?id=_id_&communityId=' + communityId
            }
        },
        i18n: {
            create: {
                title: "新增记录",
                submit: "添加"
            },
            edit: {
                title: "修改记录",
                submit: "修改",
                confirm: "您确定要修改这 %d 条记录吗?"
            },
            remove: {
                title: "删除记录",
                submit: "删除",
                confirm: "您确定要删除这 %d 条记录吗?"
            }
        },
        idSrc: 'pkId',
        table: "#dataTable",
        fields: [{
            label: "社区面积:",
            name: "communityArea"
        }, {
            label: "社区范围:",
            name: "communityScope"
        }, {
            label: "社区人口:",
            name: "communityPop"
        }, {
            label: "办公地址:",
            name: "communityAdd"
        }, {
            label: "主管领导:",
            name: "communityLeader"
        }, {
            label: "行政区号:",
            name: "communityXnum"
        }, {
            label: "固定电话:",
            name: "communityPhone"
        }, {
            label: "信息状态:",
            name: "status"
        }, {
            label: "党支部书记:",
            name: "dzbsj"
        }, {
            label: "居委会主任:",
            name: "jwhzr"
        }, {
            label: "服务站站长:",
            name: "fwzzz"
        }, {
            label: "副书记:",
            name: "fsj"
        }, {
            label: "副主任:",
            name: "fzr"
        }, {
            label: "委员:",
            name: "wy"
        }, {
            label: "户籍数:",
            name: "hjs"
        }, {
            label: "户籍人数:",
            name: "hjrs"
        }, {
            label: "常住户:",
            name: "czh"
        }, {
            label: "常住人口数:",
            name: "czrks"
        }, {
            label: "人户分离:",
            name: "rhfl"
        }, {
            label: "人在户不在:",
            name: "rzhbz"
        }, {
            label: "户在人不在:",
            name: "hzrbz"
        }, {
            label: "外来人口户:",
            name: "wlrkh"
        }, {
            label: "外来人口数:",
            name: "wlrks"
        }, {
            label: "辖区单位:",
            name: "xqdw"
        }, {
            label: "单位人数:",
            name: "dwrs"
        }, {
            label: "签字图片:",
            name: "reserveA"
        }, {
            label: "印章图片:",
            name: "reserveB"
        }
        ]
    });
    function checkNull(item,params,cparames) {
        for (var i=0;i<params.length;i++){

            var  d =item.field(params[i]);
            if (d.val() === '') {
                d.error(cparames[i]+'不能为空');
                return false;
            }
        }
        return url;

    }

    editor.on('preSubmit', function (e, d, action) {
        console.log(action)
        if (action == "remove") {
            return true;
        }
        // checkNull(this,[ "communityArea", "communityScope"],[ "社区面积", "社区范围"]);
        var communityArea = this.field('communityArea');
        var communityScope = this.field('communityScope');
        var communityPop = this.field('communityPop');
        var communityAdd = this.field('communityAdd');

        if (communityArea.val() === '') {
            communityArea.error('社区面积不能为空');
            return false;
        }
        if (communityScope.val() === '') {
            communityScope.error('社区范围不能为空');
            return false;
        }
        if (communityPop.val() === '') {
            communityPop.error('社区人口不能为空');
            return false;
        }
        if (communityAdd.val() === '') {
            communityAdd.error('办公地址不能为空');
            return false;
        }

        return true;
    });
    var buttons = [];
    var select = true;
    if (userTypeId == 1) {
        $("#communityDiv").show();
        select = true;
        buttons = [
            {extend: "create", editor: editor, text: "添加", className: 'btn btn-primary'},
            {extend: "edit", editor: editor, text: "修改", className: 'btn btn-primary'},
            {extend: "remove", editor: editor, text: "删除", className: 'btn btn-danger'}
        ];
        console.log(buttons);


    } else if (userTypeId == 2) {
        $("#communityDiv").show();
        select = true;
        buttons = [
            {extend: "edit", editor: editor, text: "修改", className: 'btn btn-primary'},
            {extend: "remove", editor: editor, text: "删除", className: 'btn btn-danger'}
        ];

    } else if (userTypeId == 3||userTypeId == 6||userTypeId == 7||userTypeId == 8||userTypeId == 9) {
        $("#communityDiv").show();
        select = false;
        buttons = [];

    } else if (userTypeId == 4) {
        $("#communityDiv").hide();
        select = false;
        buttons = [
            {extend: "create", editor: editor, text: "修改", className: 'btn btn-primary'},
        ];
    }
    table = $('#dataTable').DataTable({

        dom: "Bfrtip",
        ajax: "/commis/api/communityInfo/pageDatatables?communityId=" + communityId,

        autoWidth:false,
        responsive: true,
        serverSide: true,
        processing: true,
        lengthChange: false,//是否允许用户改变表格每页显示的记录数
        ordering: false,//是否允许用户排序
        paging: true,//是否分页
        pagingType: "full_numbers",//除首页、上一页、下一页、末页四个按钮还有页数按钮
        searching: false,//是否开始本地搜索
        stateSave: false,//刷新时是否保存状态
        // autoWidth: true,//自动计算宽度
        columnDefs: [
            { "width": "10%", "targets": 0 },
            { "width": "10%", "targets": 1 },
            { "width": "10%", "targets": 2 },
            { "width": "10%", "targets": 3 },
            { "width": "10%", "targets": 4 },
            { "width": "10%", "targets": 5 },
            { "width": "10%", "targets": 6 },
            { "width": "10%", "targets": 7 },
            { "width": "20%", "targets": 8 }

        ],
        language: {
            emptyTable: "没有数据",
            search: "搜索:",
            paginate: {
                first: "首页",
                previous: "上一页",
                next: "下一页",
                last: "尾页"

            },
            select: {
                rows: {
                    _: "选中 %d 行",
                    1: "选中 1 行"
                }/*,
                 columns: "选中 %d 列"*/
            },
            lengthMenu: "每页 _MENU_ 条记录",
            zeroRecords: "没有找到记录",
            info: "第 _PAGE_ 页 ( 总共 _PAGES_ 页 )，共 _TOTAL_ 项",
            infoEmpty: "无记录",
            infoFiltered: "(从 _MAX_ 条记录过滤)"
        },
        columns: [
            {
                data: 'community.communityName', defaultContent: ""
            }, {
                data: 'communityArea', defaultContent: ""
            }, {
                data: 'communityScope', defaultContent: ""
            }, {
                data: 'communityPop', defaultContent: ""

            }, {
                data: 'communityAdd', defaultContent: ""

            }, {
                data: 'communityLeader', defaultContent: ""

            }, {
                data: 'communityXnum', defaultContent: ""

            }, {
                data: 'communityPhone', defaultContent: ""
            }, {
                data: 'registerTime', defaultContent: ""
            }, {
                data: 'status', defaultContent: ""
            }, {
                data: 'dzbsj', defaultContent: ""
            }, {
                data: 'jwhzr', defaultContent: ""
            }, {
                data: 'fwzzz', defaultContent: ""
            }, {
                data: 'fsj', defaultContent: ""
            }, {
                data: 'fzr', defaultContent: ""
            }, {
                data: 'wy', defaultContent: ""
            }, {
                data: 'hjs', defaultContent: ""
            }, {
                data: 'hjrs', defaultContent: ""
            }, {
                data: 'czh', defaultContent: ""
            }, {
                data: 'czrks', defaultContent: ""
            }, {
                data: 'rhfl', defaultContent: ""
            }, {
                data: 'rzhbz', defaultContent: ""
            }, {
                data: 'hzrbz', defaultContent: ""
            }, {
                data: 'wlrkh', defaultContent: ""
            }, {
                data: 'wlrks', defaultContent: ""
            }, {
                data: 'xqdw', defaultContent: ""
            }, {
                data: 'dwrs', defaultContent: ""
            }, {
                data: 'reserveA', defaultContent: ""
            }, {
                data: 'reserveB', defaultContent: ""
            }], select: select,
        buttons: buttons

    });


    $('#search').on('click', function () {

        var url = "/commis/api/communityInfo/pageDatatables?1=1"

        var communityId = $('#community').val();
        url = url + "&communityId=" + communityId;

        var params = ["personName", "personPolitica", "positionId", "registerTime", "sWorkDate"];
        url = getUrl(url, params);


        table.ajax.url(url).load();
    });


    $('#userTable').on('change', 'input.editor-active', function () {
        editor
            .edit($(this).closest('tr'), false)
            .set('active', $(this).prop('checked') ? 1 : 0)
            .submit();
    });


})
//设置框架
$(function () {
    $("#index_style").frame({
        float: 'left',
        menu_nav: '.Quick_operation',
        color_btn: '.skin_select',
        Sellerber_menu: '.list_content',
        Sellerber_header: '.Sellerber_header',
    });
});
$("#iframe_box").niceScroll({
    cursorcolor: "#888888",
    cursoropacitymax: 1,
    touchbehavior: false,
    cursorwidth: "5px",
    cursorborder: "0",
    cursorborderradius: "5px"
});
/*********************/
require.config({
    paths: {
        echarts: './js/dist'
    }
});